#!/bin/bash
set -e

# nfs 共享目录
NfS_SHARED_DIRECTORY="/zpaas/data/v2"
NfS_SHARED_IP="10.10.192.0/24"
MASTER_IP="10.10.192.220"

function main() {
  #load_images
  ch0_init
  ch1_nfs
  ch2_elastic
  ch3_kibana
  ch4_kafka_zookeeper
  ch5_filebeat
  ch6_logstash
  #ch7_fluentd
}

function load_images() {
  # docker镜像在百度云
  sudo docker load < elasticsearch_7_2_0.tar.gz
  sudo docker load < fluentd.tar.gz
  sudo docker load < kibana_7_2_0.tar.gz
  sudo docker load < nfs-client-provisioner.tar.gz
  sudo docker load < nginx.tar.gz
  sudo docker load < busybox.tar.gz
  sudo docker load < kafka_2.11-0.11.0.3.tar.gz
  sudo docker load < zookeeper_3.5.tar.gz
  sudo docker load < filebeat_7.9.2.tar.gz
}


function ch0_init() {
cd ch0_init
# 对脚本的默认配置做一些修改

# 应用通过标签选择器手动调度至某几台主机上
# kubectl label node 10.10.176.85 app=elk

sudo kubectl create -f elk-namespace.yaml
sudo kubectl create -f busybox-pod.yaml
sudo kubectl get namespace|grep elk
sleep 3
sudo kubectl  get pod|grep counter
cd ..
}

function init_params() {

#上创建一个 nfs 共享目录
sudo mkdir -p $NfS_SHARED_DIRECTORY $NfS_SHARED_DIRECTORY/es $NfS_SHARED_DIRECTORY/kibana $NfS_SHARED_DIRECTORY/fluentd $NfS_SHARED_DIRECTORY/logstash  $NfS_SHARED_DIRECTORY/kafka $NfS_SHARED_DIRECTORY/zookeeper $NfS_SHARED_DIRECTORY/zookeeper
sudo chmod 777 $NfS_SHARED_DIRECTORY
sudo chmod  777 $NfS_SHARED_DIRECTORY/es
sudo chmod  777 $NfS_SHARED_DIRECTORY/kibana
sudo chmod  777 $NfS_SHARED_DIRECTORY/fluentd
sudo chmod  777 $NfS_SHARED_DIRECTORY/logstash
sudo chmod  777 $NfS_SHARED_DIRECTORY/kafka
sudo chmod  777 $NfS_SHARED_DIRECTORY/zookeeper


sudo chmod a+w /etc/exports
# ip 需要修改
sudo cat >> /etc/exports << EOF
$NfS_SHARED_DIRECTORY $NfS_SHARED_IP(rw,no_root_squash)
EOF
cat /etc/exports
#加载配置，使配置生效
sudo exportfs -arv
sudo systemctl restart nfs
sudo systemctl status nfs

# 禁用了 selfLink
# 生效
#sudo kubectl apply -f /etc/kubernetes/manifests/kube-apiserver.yaml
}

function ch1_nfs() {
cd ch1_nfs
# 安装
#sudo yum install nfs-utils -y
# 启动
#sudo systemctl start nfs
#sudo systemctl status nfs
#sudo systemctl enable nfs.service

#init_params
sed -i s@/data/v1@$NfS_SHARED_DIRECTORY@g nfs-deployment.yaml
sed -i s/192.168.40.180/$MASTER_IP/g nfs-deployment.yaml
# nfs
sudo kubectl create -f nfs-rbac.yaml
sudo kubectl create -f nfs-deployment.yaml
sudo kubectl create -f nfs-storageclass.yaml
sleep 3
sudo kubectl  get pod,svc,ep|grep  nfs
cd ..
}

function ch2_elastic() {
cd ch2_elastic
# elastic
sudo kubectl create -f elastic-cluster.yaml
sudo kubectl create -f elastic-svc.yaml
sleep 3
sudo kubectl -n elk get pod,svc,ep|grep es
cd ..
}

function ch3_kibana() {
cd ch3_kibana
# kibana
sudo kubectl create -f kibana-config.yaml
sudo kubectl create -f kibana-deploy.yaml
sudo kubectl create -f kibana-svc.yaml
sleep 3
sudo kubectl -n elk get pod,svc,ep|grep  kibana
cd ..
}

function ch4_kafka_zookeeper() {
cd ch4_kafka_zookeeper
cd ch4_1_zookeeper
# zookeeper集群部署
sudo kubectl create -f zookeeper-storage.yaml
sudo kubectl create -f zookeeper-config.yaml
sudo kubectl create -f zookeeper-pdb.yaml
sudo kubectl create -f zookeeper-svc.yaml
sudo kubectl create -f zookeeper-statefulset.yaml

cd ../ch4_2_kafka
# kafka 集群部署
sudo kubectl create -f kafka-storage.yaml
sudo kubectl create -f kafka-config.yaml
sudo kubectl create -f kafka-svc.yaml
sudo kubectl create -f kafka-statefulset.yaml

sleep 3
sudo kubectl -n elk get pod,svc,ep|grep -e kafka -e zk
cd ../..
}

function ch5_filebeat() {
cd ch5_filebeat
sudo kubectl create -f filebeat-rbac.yaml
sudo kubectl create -f filebeat-cm.yaml
# 通过挂载容器日志的方式采集日志
#sudo kubectl create -f filebeat-cm2.yaml
sudo kubectl create -f filebeat-daemonset.yaml
sleep 3
sudo kubectl -n elk get pod,svc,ep|grep filebeat
cd ..
}

function ch6_logstash() {
cd ch6_logstash
sudo kubectl create -f logstash-config.yaml
sudo kubectl create -f logstash-svc.yaml
sudo kubectl create -f logstash.yaml
sleep 3
sudo kubectl -n elk get pod,svc,ep|grep logstash
cd ..
}

function ch7_fluentd() {
cd ch7_fluentd
sudo kubectl create -f fluentd-rbac.yaml
sudo kubectl create -f fluentd-config.yaml
sudo kubectl create -f fluentd-daemonset.yaml
sleep 3
sudo kubectl -n elk get pod|grep fluentd
cd ..
}

main



